package com.bbx.taxi.client.Activity.Menu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import butterknife.Bind;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Order;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Msgs;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.End;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Location;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Locations;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrders;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Start;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.CodeNet;
import com.bbx.api.sdk.net.passeger.conn.LoginNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet_Unfini;
import com.bbx.api.sdk.net.passeger.conn.OrderDeleteNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Task.MyOrderTask;
import com.bbx.taxi.client.Task.MyOrderingTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Main.MainActivity;
import com.bbx.taxi.client.Activity.Order.OrderCancelReason;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.bbx.taxi.client.Adapter.AddressAdapter;
import com.bbx.taxi.client.Adapter.EditAddressAdapter;
import com.bbx.taxi.client.Adapter.MyOrderAdapter;
import com.bbx.taxi.client.Adapter.OrderTypeAdapter;
import com.bbx.taxi.client.Adapter.OrderTypeAdapter.ViewOrderTypeHolder;
import com.bbx.taxi.client.Adapter.Page.MyPagerAdapter;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.DB.CouponDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.Service.LoginService;
import com.bbx.taxi.client.Util.ClickFilter;
import com.bbx.taxi.client.Util.DataCleanManager;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;
import com.bbx.taxi.client.Util.NetworkDetector;
import com.bbx.taxi.client.Util.ToCity2;
import com.bbx.taxi.client.widget.MyViewPager;
import com.bbx.taxi.client.widget.Dailog.MessageDialog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyBusyDailog;
import com.bbx.taxi.client.widget.Dailog.MyPayOrderDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Dailog.MyPayOrderDailog.onClickPayListener;
import com.bbx.taxi.client.widget.StatusBar.StatusBar;
import com.yanxuwen.loadview.LoadingDialog;
import com.example.mylistview.pullListView.PullToRefreshLayout;
import com.example.mylistview.pullListView.PullToRefreshLayout.OnRefreshListener;
import com.example.mylistview.pullListView.PullableListView;
import com.example.mylistview.pullListView.PullableListView.OnLoadListener;
import com.example.mylistview.swipemenu.MyListView;
import com.example.mylistview.swipemenu.MyListView.OnMenuItemClickListener;
import com.example.mylistview.swipemenu.MyListView.OnSwipeListener;
import com.example.mylistview.swipemenu.SwipeMenu;
import com.example.mylistview.swipemenu.SwipeMenuCreator;
import com.example.mylistview.swipemenu.SwipeMenuItem;
import com.example.mylistview.swipemenu.SwipeMenuLayout;
import com.example.mylistview.swipemenu.SwipeMenuLayout.OnMenuSwitchStateListener;

public class MyOrderActivity extends BaseBBXActivity implements
		OnPageChangeListener, OnMenuSwitchStateListener {
	@Bind(R.id.viewpager)
	MyViewPager mViewPager;
	@Bind(R.id.image_bar)
	ImageView image_bar;
	@Bind(R.id.tv_allorder)
	TextView tv_allorder;
	@Bind(R.id.tv_ordering)
	TextView tv_ordering;
	@Bind(R.id.pop_background)
	View pop_background;
	private MyListView mMyListView_Ordering;
	private MyListView mMyListView_AllOrder;
	private PullToRefreshLayout pullToRefreshLayout_ing;
	private PullToRefreshLayout pullToRefreshLayout_all;
	private LinearLayout layout_nodata_ing;
	private LinearLayout layout_nodata_all;
	ArrayList<MyOrderList> list_all = new ArrayList<MyOrderList>();
	ArrayList<MyOrderList> list_ing = new ArrayList<MyOrderList>();
	private MyOrderAdapter mMyOrderAdapter_ordering;
	private MyOrderAdapter mMyOrderAdapter_allOrder;
	private int bmp_width = 0;// 图片大小
	private int bmp_height = 0;// 图片大小
	private int start_id = 0;
	private int starting_id = 0;
	private MyOrderDB db_myorder;// 我的订单数据库
	private MyOrderingDB db_myordering;// 我的进行中的订单数据库
	/** 加载跟刷新的数量 */
	public static int num_ing = 100;
	/** 加载跟刷新的数量 */
	public static int num_all = 10;
	/** 加载跟刷新的数量 */
	public static int num_all2 = 100;
	/** 进行中的订单当前最后一项的Id */
	private int last_id_ordering;
	private Handler handler = new Handler();
	private MyOrderTask mMyOrderTask;
	private MyOrderTask_Unfini mMyOrderTask_Unfini;
	private DeleteOrderTask mDeleteOrderTask;
	private boolean isFirst_ing = true;
	private boolean isFirst_all = true;
	private PopupWindow popview;
	private ListView listViewType;
	List<String> listType;
	private OrderTypeAdapter mOrderTypeAdapter;
	private int viewpageId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_menu_myorder);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		isRefreshOrder = false;
		super.onResume();
	}

	@Override
	protected void initView() {
		setTitle(getString(R.string.my_order));
		db_myorder = new MyOrderDB();
		db_myordering = new MyOrderingDB();
		initPage();
		initMyListView();
		initPopView();
		mMyOrderAdapter_ordering = new MyOrderAdapter(this, list_ing,
				mViewPager);
		mMyOrderAdapter_allOrder = new MyOrderAdapter(this, list_all,
				mViewPager);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View headerview = inflater.inflate(R.layout.headerview, null);
		// 【主要，添加headerview，则在OnItenClick中的Position就会错乱】
		mMyListView_Ordering.addHeaderView(headerview, null, false);
		mMyListView_AllOrder.addHeaderView(headerview, null, false);
		mMyListView_Ordering.setAdapter(mMyOrderAdapter_ordering);
		mMyListView_AllOrder.setAdapter(mMyOrderAdapter_allOrder);
		SwipeMenuLayout.setOnMenuSwitchStateListener(this);
		initswipe2();
		tv_allorder.setOnClickListener(this);
		tv_ordering.setOnClickListener(this);
		onReadDB_ing(true);
	}

	@Override
	protected void initData() {
	}

	@Override
	protected void desotryItems() {

	}

	private void initMyListView() {
		pullToRefreshLayout_ing.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshAllOrder(false, true);
			}
		});
		pullToRefreshLayout_all.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshAllOrder(true, true);
			}
		});
		mMyListView_Ordering.setOnLoadListener(new OnLoadListener() {

			@Override
			public void onLoad(PullableListView pullableListView) {
				loadMoreAllOrder(false);
			}
		});
		mMyListView_AllOrder.setOnLoadListener(new OnLoadListener() {

			@Override
			public void onLoad(PullableListView pullableListView) {
				loadMoreAllOrder(true);
			}
		});
		mMyListView_Ordering.setEmptyView(layout_nodata_ing);
		mMyListView_AllOrder.setEmptyView(layout_nodata_all);
		mMyListView_Ordering.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(!ClickFilter.filter(1000L))return;
				// TODO【 修正有Header的ListView的position的BUG 】
				int headerViewsCount = mMyListView_Ordering
						.getHeaderViewsCount();// 得到header的总数量
				// 【得到新的修正后的position】
				int newPosition = position - headerViewsCount;

				if (list_ing.get(newPosition).getOrder_status() < Value.ORDER_STATUS_OFFCAR
						|| list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_GAIPAI
						|| list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_KF_GAIPAI) {
					Intent intent = new Intent(context,
							StartServiceActivity.class);
					intent.putExtra(OrderMsg.extra_type, 1);
					intent.putExtra(OrderMsg.extra_order_id,
							list_ing.get(newPosition).getOrder_id());
					startActivityForResult(intent, requestCode);
				} else if (list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_FINISH
						|| list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_OFFCAR) {
					Intent intent = new Intent(context,
							OrderEvaluationActivity2.class);
					intent.putExtra(OrderMsg.order_id_finish,
							list_ing.get(newPosition).order_id);
					intent.putExtra(OrderMsg.finish_type, OrderMsg.finish_all);
					startActivityForResult(intent, requestCode);
				} else if (list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_CANCEL
						|| list_ing.get(newPosition).getOrder_status() == Value.ORDER_STATUS_KF_CANCEL) {
					Intent intent = new Intent(context, OrderCancelReason.class);
					intent.putExtra(OrderMsg.extra_order_id,
							list_ing.get(newPosition).order_id);
					startActivityForResult(intent, requestCode);

				} else {
					Intent intent = new Intent(context,
							OrderEvaluationActivity2.class);
					intent.putExtra(OrderMsg.order_id_finish,
							list_ing.get(newPosition).order_id);
					startActivityForResult(intent, requestCode);
				}

			}
		});
		mMyListView_AllOrder.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(!ClickFilter.filter(1000L))return;
				// TODO【 修正有Header的ListView的position的BUG 】
				int headerViewsCount = mMyListView_AllOrder
						.getHeaderViewsCount();// 得到header的总数量
				// 【得到新的修正后的position】
				int newPosition = position - headerViewsCount;

				if (list_all.get(newPosition).getOrder_status() < Value.ORDER_STATUS_OFFCAR
						|| list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_GAIPAI
						|| list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_KF_GAIPAI) {
					Intent intent = new Intent(context,
							StartServiceActivity.class);
					intent.putExtra(OrderMsg.extra_type, 1);
					intent.putExtra(OrderMsg.extra_order_id,
							list_all.get(newPosition).getOrder_id());
					startActivityForResult(intent, requestCode2);
				} else if (list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_FINISH
						|| list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_OFFCAR) {
					Intent intent = new Intent(context,
							OrderEvaluationActivity2.class);
					intent.putExtra(OrderMsg.order_id_finish,
							list_all.get(newPosition).order_id);
					intent.putExtra(OrderMsg.finish_type, OrderMsg.finish_all);
					startActivityForResult(intent, requestCode2);
				} else if (list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_CANCEL
						|| list_all.get(newPosition).getOrder_status() == Value.ORDER_STATUS_KF_CANCEL) {
					Intent intent = new Intent(context, OrderCancelReason.class);
					intent.putExtra(OrderMsg.extra_order_id,
							list_all.get(newPosition).order_id);
					startActivityForResult(intent, requestCode2);

				} else {
					Intent intent = new Intent(context,
							OrderEvaluationActivity2.class);
					intent.putExtra(OrderMsg.order_id_finish,
							list_all.get(newPosition).order_id);
					startActivityForResult(intent, requestCode2);
				}

			}
		});

	}

	private void initPage() {
		bmp_width = BitmapFactory
				.decodeResource(getResources(), R.drawable.bar).getWidth();// 获取图片宽度
		bmp_height = BitmapFactory.decodeResource(getResources(),
				R.drawable.bar).getHeight();// 获取图片高度度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) image_bar
				.getLayoutParams(); // 取控件textView当前的布局参数
		linearParams.height = bmp_height;

		linearParams.width = (screenW / 2);
		image_bar.setLayoutParams(linearParams);

		// **********************************
		// 加载ViewPage试图
		ArrayList<View> list = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();// 获取mInflater，用于加载布局
		View Ordering = mInflater.inflate(R.layout.activity_myorder_content,
				null);// 加载布局
		View AllOrder = mInflater.inflate(R.layout.activity_myorder_content,
				null);// 加载布局
		// View v3=mInflater.inflate(R.layout.lay3,null);//加载布局
		// 将布局加载到泛型集合
		list.add(Ordering);
		list.add(AllOrder);
		// list.add(v3);
		mViewPager.setAdapter(new MyPagerAdapter(list));// 绑定数据
		mViewPager.setCurrentItem(0);// 设置默认子试图
		mViewPager.setOnPageChangeListener(this);

		mMyListView_Ordering = (MyListView) Ordering
				.findViewById(R.id.MyListView);
		pullToRefreshLayout_ing = (PullToRefreshLayout) Ordering
				.findViewById(R.id.pullToRefreshLayout);
		layout_nodata_ing = (LinearLayout) Ordering
				.findViewById(R.id.layout_nodata);
		mMyListView_AllOrder = (MyListView) AllOrder
				.findViewById(R.id.MyListView);
		pullToRefreshLayout_all = (PullToRefreshLayout) AllOrder
				.findViewById(R.id.pullToRefreshLayout);
		layout_nodata_all = (LinearLayout) AllOrder
				.findViewById(R.id.layout_nodata);
	}

	/**
	 * 初始化ListView的Item菜单
	 */
	private void initswipe2() {

		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem openItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				openItem.setBackgroundColor(getResources()
						.getColor(R.color.red));
				// set item width
				openItem.setWidth(dp2px(65));
				// set item title
				openItem.setTitle(getString(R.string.delete));
				// set item title fontsize
				openItem.setTitleSize(18);
				// set item title font color
				openItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(openItem);

				// // create "delete" item
				// SwipeMenuItem deleteItem = new SwipeMenuItem(
				// getApplicationContext());
				// // set item background
				// deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
				// 0x3F, 0x25)));
				// // set item width
				// deleteItem.setWidth(dp2px(90));
				// // set a icon
				// deleteItem.setIcon(R.drawable.ic_delete);
				// // add to menu
				// menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		// mMyListView_Ordering.setMenuCreator(creator);
		mMyListView_AllOrder.setMenuCreator(creator);

		// step 2. listener item click event
		// mMyListView_Ordering
		// .setOnMenuItemClickListener(new OnMenuItemClickListener() {
		// @Override
		// public void onMenuItemClick(int position, SwipeMenu menu,
		// int index) {
		// // ApplicationInfo item = mAppList.get(position);
		// switch (index) {
		// case 0:
		// break;
		//
		// }
		// }
		// });
		mMyListView_AllOrder
				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public void onMenuItemClick(final int position,
							SwipeMenu menu, int index) {
						// ApplicationInfo item = mAppList.get(position);
						switch (index) {
						case 0:
							final MyAlertDailog dialog = new MyAlertDailog(
									context);
							Resources res = context.getResources();
							dialog.setTitle(res
									.getString(R.string.mydailog_title));
							dialog.setContent(res
									.getString(R.string.mydailog_content_delete));
							dialog.setLeftButtonText(res
									.getString(R.string.mydailog_close));
							dialog.setRightButtonText(res
									.getString(R.string.mydailog_delete));
							if (dialog != null && !dialog.isShowing()) {
								dialog.show();
							}
							dialog.setOnClickLeftListener(new onClickLeftListener() {
								@Override
								public void onClickLeft() {
									dialog.dismiss();
								}
							});
							dialog.setOnClickRightListener(new onClickRightListener() {

								@Override
								public void onClickRight() {
									dialog.dismiss();
									if (list_all.get(position)
											.getOrder_status() <= Value.ORDER_STATUS_OFFCAR
											&& list_all.get(position)
													.getOrder_status() != Value.ORDER_STATUS_GAIPAI
											&& list_all.get(position)
													.getOrder_status() != Value.ORDER_STATUS_KF_GAIPAI) {
										ToastUtil.showToast(context,
												R.string.ordering_no_delete);
										handler.postDelayed(new Runnable() {
											@Override
											public void run() {
												mMyListView_AllOrder
														.closeMenu();
											}
										}, 200);
									} else {
										Order mOrder = new Order(context);
										mOrder.uid = mApplication.getUid();
										mOrder.access_token = mApplication
												.getToken();
										mOrder.order_id = list_all
												.get(position).order_id;
										mDeleteOrderTask = new DeleteOrderTask(
												context, mOrder, position);
										mDeleteOrderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
									}
								}
							});
							break;

						}
					}
				});
		// set SwipeListener
		// mMyListView_Ordering.setOnSwipeListener(new OnSwipeListener() {
		//
		// @Override
		// public void onSwipeStart(int position) {
		// // swipe start
		// }
		//
		// @Override
		// public void onSwipeEnd(int position) {
		// }
		// });
		mMyListView_AllOrder.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				pullToRefreshLayout_all.setIsProhibitPull(true);
			}

			@Override
			public void onSwipeEnd(int position) {
				pullToRefreshLayout_all.setIsProhibitPull(false);
			}
		});
		mMyListView_Ordering.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				pullToRefreshLayout_ing.setIsProhibitPull(true);
			}

			@Override
			public void onSwipeEnd(int position) {
				pullToRefreshLayout_ing.setIsProhibitPull(false);
			}
		});
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	private void initPopView() {
		View view = LayoutInflater.from(context).inflate(
				R.layout.pop_order_type, null);
		popview = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		popview.setBackgroundDrawable(new BitmapDrawable());
		popview.setContentView(view);
		popview.setOutsideTouchable(false);
		popview.setFocusable(true);
		popview.setAnimationStyle(R.style.mypopwindow_anim_style_instead);
		popview.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				pop_background.setVisibility(View.GONE);
			}
		});
		listViewType = (ListView) view.findViewById(R.id.listview);
		listType = new ArrayList<String>();
		listType.add(getString(R.string.all_order));
		listType.add(getString(R.string.ordering));
		listType.add(getString(R.string.finish_order));
		listType.add(getString(R.string.cancel_order));
		mOrderTypeAdapter = new OrderTypeAdapter(context, listType);
		mOrderTypeAdapter.isSelected.put(0, true);
		listViewType.setAdapter(mOrderTypeAdapter);
		listViewType.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listViewType
				.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View v,
							int position, long arg3) {
						for (int i = 0; i < listType.size(); i++) {
							if (i == position) {
								mOrderTypeAdapter.isSelected.put(i, true);
							} else {
								mOrderTypeAdapter.isSelected.put(i, false);
							}
						}
						mOrderTypeAdapter.notifyDataSetChanged();
						ViewOrderTypeHolder vHollder = (ViewOrderTypeHolder) v
								.getTag();
						// 在每次获取点击的item时将对于的checkbox状态改变，同时修改map的值。
						if (!vHollder.checkbox.isChecked()) {
							vHollder.checkbox.toggle();
						}
						popview.dismiss();
						setTitle(listType.get(position));
					}
				});
	}

	/**
	 * 刷新全部的订单，isPull判断是否下啦刷新还是，自动刷新
	 */
	public void refreshAllOrder(boolean isAll, boolean isPull) {
		if (!NetworkDetector.detect(context)){
			pullToRefreshLayout_all.refreshFinish(PullToRefreshLayout.FAIL);
	        ToastUtil.showToast(context, R.string.no_network);
	        if (loaddialog != null) {
				loaddialog.onDismiss();
			}
			return;
		}
		if (isAll)
			start_id = 0;
		else
			starting_id = 0;
		MyOrder myOrder = new MyOrder(context);
		myOrder.uid = mApplication.getUid();
		myOrder.access_token = mApplication.getToken();
		myOrder.start_id = isAll ? start_id : starting_id;
		myOrder.count = isAll ? num_all : num_ing;
		myOrder.direction = -1;
		if (isAll) {
			mMyListView_AllOrder.setSlide(false);
			mMyOrderTask = new MyOrderTask(context, myOrder, 0, isPull);
			mMyOrderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else {
			mMyListView_Ordering.setSlide(false);
			mMyOrderTask_Unfini = new MyOrderTask_Unfini(context, myOrder, 0,
					isPull);
			mMyOrderTask_Unfini.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}

	}

	private void loadMoreAllOrder(boolean isAll) {
		if (!NetworkDetector.detect(context)){
			mMyListView_AllOrder.setHasMoreData(false);
			 ToastUtil.showToast(context, R.string.no_network);
			return;
		}
		MyOrder myOrder = new MyOrder(context);
		myOrder.uid = mApplication.getUid();
		myOrder.access_token = mApplication.getToken();
		myOrder.start_id = isAll ? start_id : starting_id;
		myOrder.count = isAll ? num_all : num_ing;
		myOrder.direction = 0;
		if (isAll) {
			mMyOrderTask = new MyOrderTask(context, myOrder, 1, false);
			mMyOrderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else {
			mMyOrderTask_Unfini = new MyOrderTask_Unfini(context, myOrder, 1,
					false);
			mMyOrderTask_Unfini.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	};

	/**
	 * 全部订单
	 */
	private class MyOrderTask extends BaseAsyncTask {

		private MyOrder myOrder;
		private int type;// 0为刷新，1为加载
		LoadingDialog dialog;
		private boolean isPull;

		public MyOrderTask(Activity context, MyOrder myOrder, int type,
				boolean isPull) {
			super(context, false);
			this.myOrder = myOrder;
			this.isPull = isPull;
			setType(type);
		}

		@Override
		public void onSuccess(int status,Object object) {
			if (object != null) {
				final MyOrders myOrders = (MyOrders) object;
				if (getType() == 0) {// 刷新
					list_all.clear();
					int size = 0;
					if (myOrders.getList() != null
							&& !myOrders.getList().isEmpty()) {
						start_id = myOrders.getList()
								.get(myOrders.getList().size() - 1).getId();
						list_all.addAll(myOrders.getList());
						onWriteDB(list_all, true);
						size = myOrders.getList().size();
					} else {
						onDeleteDB();
					}
					notifyDataSetChanged(mMyListView_AllOrder,
							mMyOrderAdapter_allOrder, size, num_all,true);
					if (isPull) {
						pullToRefreshLayout_all
								.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					if (loaddialog != null) {
						loaddialog.onDismiss();
					}
				} else if (getType() == 1) {// 加载
					int size = 0;
					if (myOrders.getList() != null
							&& !myOrders.getList().isEmpty()) {
						start_id = myOrders.getList()
								.get(myOrders.getList().size() - 1).getId();
						list_all.addAll(myOrders.getList());
						onWriteDB(list_all, true);
						size = myOrders.getList().size();
						notifyDataSetChanged(mMyListView_AllOrder,
								mMyOrderAdapter_allOrder, size, num_all,false);
					} else {
						mMyListView_AllOrder.setHasMoreData(false);
						ToastUtil.showToast(context,
								getString(R.string.in_the_end));
					}

				}
			} else {
				if (getType() == 0) {// 刷新
					pullToRefreshLayout_all
							.refreshFinish(PullToRefreshLayout.FAIL);
					ToastUtil.showToast(context, R.string.no_network);
				} else if (getType() == 1) {// 加载
					ToastUtil.showToast(context, R.string.load_fail);
				}
				if (loaddialog != null) {
					loaddialog.onDismiss();
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			if (getType() == 0) {// 刷新
				pullToRefreshLayout_all.refreshFinish(PullToRefreshLayout.FAIL);
				ToastUtil.showToast(context, R.string.no_network);
			} else if (getType() == 1) {// 加载
				ToastUtil.showToast(context, R.string.load_fail);
			}
			if (loaddialog != null) {
				loaddialog.onDismiss();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(myOrder).getJson();
			return new MyOrderNet(context, json);
		}
	}

	/**
	 * 进行中的订单
	 */
	private class MyOrderTask_Unfini extends BaseAsyncTask {

		private MyOrder myOrder;
		private int type;// 0为刷新，1为加载
		private boolean isPull;

		public MyOrderTask_Unfini(Activity context, MyOrder myOrder, int type,
				boolean isPull) {
			super(context, false);
			this.myOrder = myOrder;
			this.isPull = isPull;
			setType(type);
		}

		@Override
		public void onSuccess(int status,Object object) {
			if (object != null) {
				final MyOrders myOrders = (MyOrders) object;
				if (getType() == 0) {// 刷新
					list_ing.clear();
					int size = 0;
					if (myOrders.getList() != null
							&& !myOrders.getList().isEmpty()) {
						starting_id = myOrders.getList()
								.get(myOrders.getList().size() - 1).getId();
						// 优先排序已下车，待支付
						ArrayList<MyOrderList> list_sort = new ArrayList<MyOrderList>();
						for (int i = 0; i < myOrders.getList().size(); i++) {
							if (myOrders.getList().get(i).getOrder_status() == Value.ORDER_STATUS_OFFCAR) {
								list_sort.add(myOrders.getList().get(i));
							}
						}
						for (int i = 0; i < myOrders.getList().size(); i++) {
							if (myOrders.getList().get(i).getOrder_status() != Value.ORDER_STATUS_OFFCAR) {
								list_sort.add(myOrders.getList().get(i));
							}
						}

						list_ing.addAll(list_sort);
						onWriteDB_ing(myOrders.getList());
						if (isPull) {
							if (myOrders.getList().size() >= num_ing) {
								mMyListView_Ordering.setHasMoreData(true);
							} else {
								mMyListView_Ordering.setHasMoreData(false);
							}
						}
						size = myOrders.getList().size();
					} else {
						onDeleteDB_ing();
					}
					notifyDataSetChanged(mMyListView_Ordering,
							mMyOrderAdapter_ordering, size, num_ing,true);
					if (isPull) {
						pullToRefreshLayout_ing
								.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					if (loaddialog != null) {
						loaddialog.onDismiss();
					}
				} else if (getType() == 1) {// 加载
					int size = 0;
					if (myOrders.getList() != null
							&& !myOrders.getList().isEmpty()) {
						starting_id = myOrders.getList()
								.get(myOrders.getList().size() - 1).getId();
						list_ing.addAll(myOrders.getList());
						size = myOrders.getList().size();
						notifyDataSetChanged(mMyListView_Ordering,
								mMyOrderAdapter_ordering, size, num_ing,false);
					} else {

						mMyListView_Ordering.setHasMoreData(false);
						ToastUtil.showToast(context, R.string.in_the_end);
					}

				}

			} else {
				if (getType() == 0) {// 刷新
					pullToRefreshLayout_ing
							.refreshFinish(PullToRefreshLayout.FAIL);
					ToastUtil.showToast(context, R.string.no_network);
				} else if (getType() == 1) {// 加载
					ToastUtil.showToast(context, R.string.load_fail);
				}
				if (loaddialog != null) {
					loaddialog.onDismiss();
				}
			}

		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			if (getType() == 0) {// 刷新
				pullToRefreshLayout_ing.refreshFinish(PullToRefreshLayout.FAIL);
				ToastUtil.showToast(context, R.string.no_network);
			} else if (getType() == 1) {// 加载
				ToastUtil.showToast(context, R.string.load_fail);
			}
			if (loaddialog != null) {
				loaddialog.onDismiss();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(myOrder).getJson();
			return new MyOrderNet_Unfini(context, json);
		}
	}

	public class DeleteOrderTask extends BaseAsyncTask {

		private Order order;

		private int delete_id;

		public DeleteOrderTask(Activity context, Order order, int delete_id) {
			super(context);
			this.order = order;
			this.delete_id = delete_id;
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(order).getJson();
			return new OrderDeleteNet(context, json);
		}

		@Override
		public void onSuccess(int status,Object object) {
			ToastUtil.showToast(context, getString(R.string.del_success));
			db_myorder.onDelete(context, list_all.get(0).getOrder_id());
			list_all.remove(delete_id);
			notifyDataSetChanged(mMyListView_AllOrder, mMyOrderAdapter_allOrder);

			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					mMyListView_AllOrder.closeMenu();
				}
			}, 200);
		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			ToastUtil.showToast(context, getString(R.string.del_fail));
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					mMyListView_AllOrder.closeMenu();
				}
			}, 200);
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		viewpageId = arg0;
		Animation animation = null;
		switch (arg0) {
		case 0:
			animation = AnimationUtils.loadAnimation(this,
					R.anim.viewpagebar_right);
			tv_ordering.setTextColor(getResources().getColor(R.color.btn_nor));
			tv_allorder.setTextColor(getResources().getColor(R.color.black));
			if (isFirst_ing)
				onReadDB_ing(true);
			break;
		case 1:
			animation = AnimationUtils.loadAnimation(this,
					R.anim.viewpagebar_left);
			tv_ordering.setTextColor(getResources().getColor(R.color.black));
			tv_allorder.setTextColor(getResources().getColor(R.color.btn_nor));
			if (isFirst_all)
				onReadDB(true);
			break;

		}
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		image_bar.startAnimation(animation);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_ordering:
			mViewPager.setNoScroll(false);
			mViewPager.setCurrentItem(0);
			break;
		case R.id.tv_allorder:
			mViewPager.setNoScroll(false);
			mViewPager.setCurrentItem(1);
			break;
		case R.id.tv_title:
			// popview.showAsDropDown(v);
			// pop_background.setVisibility(View.VISIBLE);
			// AlphaAnimation animation = new AlphaAnimation(0f, 1f);
			// animation.setDuration(100);
			// LayoutAnimationController controller = new
			// LayoutAnimationController(animation, 1f);
			// controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			// listViewType.setLayoutAnimation(controller);
			// mOrderTypeAdapter.notifyDataSetChanged();
			break;
		default:
			super.onClick(v);
			break;
		}
	}

	@Override
	public void OnMenuSwitchState(boolean state) {
		if (state == false) {
			mViewPager.setNoScroll(false);
		}
	}

	/**
	 * 读取进行中的订单数据库数据,isLoad=true则出现加载框
	 */
	private void onReadDB_ing(final boolean isLoad) {
		mMyListView_Ordering.setSlide(false);
		int delayMillis=0;
		if(mMyListView_Ordering.getFirstVisiblePosition() != 0){
			mMyListView_Ordering.setSelection(0);
			delayMillis=1000;
		}
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				new AsyncTask<Void, Void, Void>() {
					protected void onPreExecute() {
						if (loaddialog != null && !loaddialog.isShowing() && isLoad) {
							loaddialog.show();
						}
						super.onPreExecute();
					}

					@Override
					protected Void doInBackground(Void... params) {
						list_ing.clear();
						list_ing.addAll(db_myordering.onReadDB_ing(num_ing,
								false));
						return null;
					}

					protected void onPostExecute(Void result) {
						notifyDataSetChanged(mMyListView_Ordering,
								mMyOrderAdapter_ordering, list_ing.size(), num_ing,true);
						if (isFirst_ing) {
							isFirst_ing = false;
							refreshAllOrder(false, false);
						} else {
							if (loaddialog != null) {
								loaddialog.onDismiss();
							}
						}
					}
				}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			}
		}, delayMillis);
	}

	/**
	 * 读取全部订单数据库数据,根据类型，isLoad=true则出现加载框
	 */
	private void onReadDB(final boolean isLoad) {
		mMyListView_AllOrder.setSlide(false);
		int delayMillis=0;
		if(mMyListView_AllOrder.getFirstVisiblePosition() != 0){
			mMyListView_AllOrder.setSelection(0);
			delayMillis=2000;
		}
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				new AsyncTask<Void, Void, Void>() {
					protected void onPreExecute() {
						if (loaddialog != null && !loaddialog.isShowing() && isLoad) {
							loaddialog.show();
						}
						super.onPreExecute();
					}

					@Override
					protected Void doInBackground(Void... params) {
						list_all.clear();
						list_all.addAll(db_myorder.onReadDB(num_all));	
						return null;
					}

					protected void onPostExecute(Void result) {
						notifyDataSetChanged(mMyListView_AllOrder,
								mMyOrderAdapter_allOrder, list_all.size(), num_all,true);
						if (isFirst_all) {
							isFirst_all = false;
							refreshAllOrder(true, false);
						} else {
							if (loaddialog != null) {
								loaddialog.onDismiss();
							}
						}

						if (list_all.size() >= num_all) {
							start_id = list_all.get(list_all.size() - 1).getId();
							mMyListView_AllOrder.setHasMoreData(true);
						}
					};
				}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			}
		}, delayMillis);
	}

	/**
	 * 写入进行中的订单数据
	 */
	private void onWriteDB_ing(ArrayList<MyOrderList> list) {
		isWriteIngFinish = false;
		db_myordering.onWriteDB_ing(list, mHandler);
	}

	/**
	 * 写入全部订单数据
	 */
	private void onWriteDB(ArrayList<MyOrderList> list, boolean isDelete) {
		isWriteFinish = false;
		db_myorder.onWriteDB(list, isDelete, mHandler);
	}

	/**
	 * 删除进行中的所有数据
	 */
	public void onDeleteDB_ing() {
		db_myordering.onDelete();
		// db_locations.onDelete();
	}

	/**
	 * 删除全部所有数据
	 */
	public void onDeleteDB() {
		db_myorder.onDelete();
		// db_locations.onDelete();
	}

	@Override
	protected void onActivityResult(final int requestCode, int resultCode,
			Intent data) {
		if (requestCode == this.requestCode || requestCode == this.requestCode2) {

			if (requestCode == this.requestCode) {
				if (list_ing.size() > 0) {
					mMyOrderAdapter_ordering.notifyDataSetChanged();
					mMyListView_Ordering.setSelection(0);
				}
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						onReadDB_ing(true);
					}
				}, 300);

			}
			if (requestCode == this.requestCode2) {
				if (list_all.size() > 0) {
					mMyOrderAdapter_allOrder.notifyDataSetChanged();
					mMyListView_AllOrder.setSelection(0);
				}
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						onReadDB(true);
					}
				}, 300);

			}

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	protected void onDestroy() {
		if (mMyOrderTask != null
				&& mMyOrderTask.getStatus() != AsyncTask.Status.FINISHED)
			mMyOrderTask.cancel(true);
		if (mMyOrderTask_Unfini != null
				&& mMyOrderTask_Unfini.getStatus() != AsyncTask.Status.FINISHED)
			mMyOrderTask_Unfini.cancel(true);
		if (mDeleteOrderTask != null
				&& mDeleteOrderTask.getStatus() != AsyncTask.Status.FINISHED)
			mDeleteOrderTask.cancel(true);
		super.onDestroy();

	}

	public void notifyDataSetChanged(final MyListView mMyListView,
			final BaseAdapter adapter, int size, int num,boolean refresh) {
		onisShowNodata(adapter);
		if (size >= num) {
			mMyListView.setHasMoreData(true);
		} else {
			mMyListView.setHasMoreData(false);
		}
		adapter.notifyDataSetChanged();
		if(refresh)
		mMyListView.setSlide(true);
	}

	/**
	 * 只用于删除
	 */
	public void notifyDataSetChanged(final MyListView mMyListView,
			final BaseAdapter adapter) {
		adapter.notifyDataSetChanged();
		onisShowNodata(adapter);

	}

	public void onisShowNodata(BaseAdapter adapter) {
		// if (adapter.getCount() == 0) {
		// if (adapter == mMyOrderAdapter_allOrder) {
		// layout_nodata_all.setVisibility(View.VISIBLE);
		// } else {
		// layout_nodata_ing.setVisibility(View.VISIBLE);
		// }
		//
		// } else {
		// if (adapter == mMyOrderAdapter_allOrder) {
		// layout_nodata_all.setVisibility(View.GONE);
		// } else {
		// layout_nodata_ing.setVisibility(View.GONE);
		// }
		// }
	}

	@Override
	public void onNotify(int type, JSONObject jb, Msgs.Data msgs_data) {
		/** 是否继续执行父类 */
		boolean isContinue = true;
		switch (type) {
		case GMsg.ONLINE_PAIDAN:
		case GMsg.ONLINE_CANCEL:
		case GMsg.ONLINE_KF_CANCEL:
		case GMsg.ONLINE_GAIPAI:
		case GMsg.ONLINE_KF_GAIPAIL:
		case GMsg.ONLINE_SJ_GAIPAIL: 
		case GMsg.ONLINE_ONCAR: 
		case GMsg.ONLINE_FINISH:
			isContinue=false;
			isFirst_all = true;
			isFirst_ing = true;
			if (viewpageId == 0) {
				onReadDB_ing(false);
			} else {
				onReadDB(false);
			}
			super.onNotify(type, jb, msgs_data);
			break;
		case GMsg.ONLINE_OFFCAR:
			isContinue=false;
			isFirst_all = true;
			isFirst_ing = true;
			if (viewpageId == 0) {
				onReadDB_ing(true);
			} else {
				onReadDB(true);
			}

			if (msgs_data != null) {
				final String order_id = msgs_data.getOrder_id();
				MyOrderingDB db_myordering = new MyOrderingDB();// 我的进行中的订单数据库
				MyOrderList mMyOrderList = db_myordering.onReadDB_ing(
						order_id);
				String start_city = ToCity2.getCity(mMyOrderList.getLocations()
						.getStart().getAddress());
				String end_city = ToCity2.getCity(mMyOrderList.getLocations()
						.getEnd().getAddress());
				final MyPayOrderDailog payorder_dialog = new MyPayOrderDailog(
						context);
				payorder_dialog.setOrderId(order_id);
				payorder_dialog.setMoney(FormatUtil.onFormatPrice(mMyOrderList
						.getPrice()));
				payorder_dialog.setContent(String.format(
						getString(R.string.order),
						DateFormat.onDateRemove_ss(mMyOrderList
								.getAppoint_time())
								+ "\n"
								+ start_city
								+ getString(R.string.to)
								+ end_city));
				if (payorder_dialog != null && !payorder_dialog.isShowing()) {
					payorder_dialog.show();
				}
				payorder_dialog.setOnClickPayListener(new onClickPayListener() {
					@Override
					public void onClickPay() {
						Intent intent = new Intent(context,
								OrderEvaluationActivity2.class);
						intent.putExtra(OrderMsg.order_id_finish, order_id);
						intent.putExtra(OrderMsg.finish_type,
								OrderMsg.finish_ing);
						context.startActivityForResult(intent,
								viewpageId == 0 ? requestCode : requestCode2);
					}
				});
			}
			break;
		case GMsg.ONLINE_BUSY:
			isContinue=false;
			String text = null;
			try {
				JSONObject json_jmsg = jb.getJSONObject(GMsg.msg_jmsg);
				JSONObject jmsg_data = new JSONObject(
						json_jmsg.getString(GMsg.msg_data));
				msgs_data = (Msgs.Data) new JsonBuild().getData(
						Msgs.Data.class, jmsg_data.toString());
			} catch (JSONException e1) {
			}
			if (msgs_data != null && msgs_data.getText() != null) {
				MyOrderingDB db_myordering = new MyOrderingDB();// 我的进行中的订单数据库
				MyOrderList mMyOrderList = db_myordering.onReadDB_ing(
						msgs_data.getOrder_id());
				String start_city = ToCity2.getCity(mMyOrderList.getLocations()
						.getStart().getAddress());
				String end_city = ToCity2.getCity(mMyOrderList.getLocations()
						.getEnd().getAddress());
				MyBusyDailog busyDailog = new MyBusyDailog(this);
				busyDailog.setTitle(msgs_data.getText());
				busyDailog.setContent(DateFormat.onDateRemove_ss(mMyOrderList
						.getAppoint_time())
						+ "\n"
						+ start_city
						+ "→"
						+ end_city);
				if (busyDailog != null && !busyDailog.isShowing()) {
					busyDailog.show();
				}
			}

			break;
		case GMsg.ONLINE_BG_ORDER:
			isContinue=false;
			isFirst_all = true;
			isFirst_ing = true;
			if (viewpageId == 0) {
				onReadDB_ing(true);
			} else {
				onReadDB(true);
			}

			break;
		case GMsg.ONLINE_RELOGIN:
			isContinue=false;
			stopService(new Intent(this, LoginService.class));
			MyAlertDailog dialog = new MyAlertDailog(this);
			Resources res = context.getResources();
			dialog.setTitle(res.getString(R.string.mydailog_title));
			dialog.setContent(res.getString(R.string.mydailog_content_login));
			dialog.setLeftButtonText(res.getString(R.string.mydailog_exit));
			dialog.setRightButtonText(res.getString(R.string.mydailog_relogin));
			if (dialog != null && !dialog.isShowing()) {
				dialog.show();
			}
			dialog.setOnClickLeftListener(new onClickLeftListener() {
				@Override
				public void onClickLeft() {
					stopService(new Intent(context, LoginService.class));
					DataCleanManager.cleanDatabases(context);
					DataCleanManager.cleanSharedPreference(context);
					Intent intent = new Intent(context, MainActivity.class);
					intent.putExtra(MainMsg.extra_quit, MainMsg.INTENT_QUIT);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					overridePendingTransition(0, 0);
					finish();
					overridePendingTransition(0, 0);
				}
			});
			dialog.setOnClickRightListener(new onClickRightListener() {

				@Override
				public void onClickRight() {
					Intent intent = new Intent();
					intent.setClass(context, LoginActivity.class);
					startActivity(intent);
				}
			});
			break;
		case GMsg.ONLINE_KF_CHANGE:
			isContinue=false;
			ToastUtil.showToast(context, R.string.toast_change);
			isFirst_all = true;
			isFirst_ing = true;
			if (viewpageId == 0) {
				onReadDB_ing(false);
			} else {
				onReadDB(false);
			}
			break;
		case GMsg.ONLINE_KF_CHANGE_ORDER:
		case GMsg.ONLINE_KF_CHANGE_PRICE:
		case GMsg.ONLINE_KF_CHANGE_PRICE2:
			isContinue=false;
			MessageDialog.onChangeDialog(context, type,msgs_data);
			isFirst_all = true;
			isFirst_ing = true;
			if (viewpageId == 0) {
				onReadDB_ing(false);
			} else {
				onReadDB(false);
			}
			break;
		}
		if (isContinue) {
			super.onNotify(type, jb, msgs_data);
		}

	}

	@Override
	public void onOrderingFinish(ArrayList<MyOrderList> list) {
		if (list != null) {
			list_ing.clear();
			list_ing.addAll(list);
			notifyDataSetChanged(mMyListView_Ordering,
					mMyOrderAdapter_ordering, list_ing.size(), num_ing,true);
		}
	}

	@Override
	public void onOrderFinish(ArrayList<MyOrderList> list) {
		if (list != null) {
			list_all.clear();
			list_all.addAll(list);
			notifyDataSetChanged(mMyListView_AllOrder,
					mMyOrderAdapter_allOrder, list_all.size(), num_all,true);
		}
	}
	/**由于中途取消加载，马上点击Touch事件的话会出发下啦导致奔溃，所以500毫秒内，不能点击*/
    public boolean isTouch=true;
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(!isTouch){
			return true;
		}
		return super.dispatchTouchEvent(ev);
	}
	@Override
	public void onDismiss(DialogInterface dialog) {
		isTouch=false;
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				isTouch=true;
			}
		}, 500);
		if (mMyOrderTask != null
				&& mMyOrderTask.getStatus() != AsyncTask.Status.FINISHED)
			mMyOrderTask.cancel(true);
		if (mMyOrderTask_Unfini != null
				&& mMyOrderTask_Unfini.getStatus() != AsyncTask.Status.FINISHED)
			mMyOrderTask_Unfini.cancel(true);
		if (mDeleteOrderTask != null
				&& mDeleteOrderTask.getStatus() != AsyncTask.Status.FINISHED)
			mDeleteOrderTask.cancel(true);
		super.onDismiss(dialog);
	}
}
